var btn = document.getElementById('btn');
var carousel = document.getElementById('carousel');

for(var i=0; i<btn.children.length; i++) {
    btn.children[i].index = i;  //保存索引
    btn.children[i].onmouseover = function(){
        for(var j=0; j<btn.children.length; j++){
            btn.children[j].className = '';
        }
        this.className = 'current';
        var target = - this.index * 270;
        animate(carousel, target);
    }
}


// 动画效果
function animate(obj,target){
    clearInterval(obj.timer);
    obj.timer = setInterval(function(){
        var leader = obj.offsetLeft;
        var step = 30;
        step = leader < target ? step : -step;
        if(Math.abs(leader - target) >= Math.abs(step)){
            leader += step;
            obj.style.left = leader + 'px';
        } else {
            obj.style.left = target + 'px';
            clearInterval(obj.timer);
        }
    },15)
}